package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import engine.GameState;

public final class MapTestState extends GameState {

	Tile[] tiles;

	int width = 900;
	int height = 600;
	int tileW = 15;
	int tileH = 15;

	BufferedImage[] cloud = null;
	Point2D[] clouds = new Point2D[12];
	int cloudSpeed = 1;

	@Override
	public void update(long delay) {
		for (int i = 0, l = clouds.length; i < l; i++) {
			clouds[i].x -= cloudSpeed;
			if (clouds[i].x < -300)
				clouds[i] = new Point2D(width
						+ (int) (Math.random() * (width * 3)),
						(int) (Math.random() * (height + 50)) - 25,
						cloud[(int) (Math.random() * 3.9)]);
		}
	}

	@Override
	public void paint(Graphics g) {

		int total = tiles.length;
		for (int i = 0, j = 0; i < total; i++) {

			j = ((i * tileW) / width) * tileH;

			g.setColor(new Color(tiles[i].color));

			g.fillRect((i * tileW) % width, j, tileW, tileH);
			if (tiles[i].tileComponent != null) {
				g.drawImage(tiles[i].tileComponent, (i * tileW) % width, j - 3,
						null);
			}
		}
		for (int i = 0, l = clouds.length; i < l; i++) {
			g.drawImage(clouds[i].cloud, clouds[i].x, clouds[i].y, null);

		}

	}

	final class Point2D {
		BufferedImage cloud;
		int x, y;

		Point2D(int x, int y, BufferedImage cloud) {
			this.x = x;
			this.y = y;
			this.cloud = cloud;
		}
	}

	@Override
	public void init() {

	}

	@Override
	public void unload() {
		System.out.println("UNLOADED");

	}

	@Override
	public void load() {
		BufferedImage tree = null;
		BufferedImage stones = null;

		try {
			tree = ImageIO.read(new File("Three-15.png"));
			stones = ImageIO.read(new File("stones.png"));

			cloud = new BufferedImage[] {
					ImageIO.read(new File("cloud-0.png")),
					ImageIO.read(new File("cloud-1.png")),
					ImageIO.read(new File("cloud-2.png")),
					ImageIO.read(new File("cloud-3.png")) };
		} catch (IOException e) {
			System.out.println(e);
		}

		for (int i = 0, l = clouds.length; i < l; i++) {

			clouds[i] = new Point2D((int) (Math.random() * (width * 3)),
					(int) (Math.random() * (height + 50)) - 25,
					cloud[(int) (Math.random() * 3.9)]);
		}

		int total = width * height / (tileW * tileH);
		System.out.println(total);
		tiles = new Tile[total];
		int color = 0;
		boolean hasTree, hasStone;
		for (int i = 0; i < total; i++) {
			hasTree = Math.random() * 100 > 97;
			hasStone = Math.random() * 100 > 99;
			color = hasTree ? 0x9900 : ((int) Math.round(Math.random()
					* (0xbc - 0x99))) + 0x99 << 8;
			// color = 0x9900;
			tiles[i] = new Tile(color, hasTree ? tree : (hasStone ? stones
					: null));
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		System.out.println("mouseClicked x=" + e.getX() + " y=" + e.getY());

	}

	@Override
	public void mousePressed(MouseEvent e) {
		cloudSpeed++;
		System.out.println("Mouse pressed x=" + e.getX() + " y=" + e.getY());

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		System.out.println("Mouse released x=" + e.getX() + " y=" + e.getY());

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		System.out.println("Mouse entered x=" + e.getX() + " y=" + e.getY());

	}

	@Override
	public void mouseExited(MouseEvent e) {
		System.out.println("Mouse exited x=" + e.getX() + " y=" + e.getY());

	}

	@Override
	public void keyTyped(KeyEvent e) {

		System.out.println("keyTyped " + KeyEvent.getKeyText(e.getKeyCode()));

	}

	@Override
	public void keyPressed(KeyEvent e) {
		cloudSpeed--;
		System.out.println("keyPressed " + KeyEvent.getKeyText(e.getKeyCode()));

	}

	@Override
	public void keyReleased(KeyEvent e) {
		System.out
				.println("keyReleased " + KeyEvent.getKeyText(e.getKeyCode()));

	}

}
